/*
 * Copyright 2008-2010 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package jrebel.client;

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.URI;
import java.net.UnknownHostException;

import com.sun.tools.attach.VirtualMachine;

/**
 * This class represents a BTrace client. This can be used to create command
 * line as well as a GUI based BTrace clients. The BTrace compilation, traced
 * JVM attach, submission of compiled program and and I/O to the traced JVM are
 * handled by this class.
 * 
 * @author A. Sundararajan
 */
public class Client {
	private volatile Socket sock;
	private volatile ObjectInputStream ois;
	private volatile ObjectOutputStream oos;
	private int port ;

	public Client(int port) {
		this.port = port ;
	}




	/**
	 * Attach the BTrace client to the given Java process. Loads BTrace agent on
	 * the target process if not loaded already.
	 */
	public void attach(String pid) throws IOException {
		try {
			String agentPath = "/jrebel-agent.jar";
			String tmp = Client.class.getClassLoader()
					.getResource("jrebel/agent").toString();
			System.out.println(tmp) ;
			tmp = tmp.substring(0, tmp.indexOf("!"));
			tmp = tmp.substring("jar:".length(), tmp.lastIndexOf("/"));
			agentPath = tmp + agentPath;
			agentPath = new File(new URI(agentPath)).getAbsolutePath();
			attach(pid, agentPath, null, null);
		} catch (RuntimeException re) {
			throw re;
		} catch (IOException ioexp) {
			throw ioexp;
		} catch (Exception exp) {
			throw new IOException(exp.getMessage());
		}
	}

	/**
	 * Attach the BTrace client to the given Java process. Loads BTrace agent on
	 * the target process if not loaded already. Accepts the full path of the
	 * btrace agent jar. Also, accepts system classpath and boot classpath
	 * optionally.
	 */
	public void attach(String pid, String agentPath, String sysCp, String bootCp)
			throws IOException {
		try {
			VirtualMachine vm = null;
			vm = VirtualMachine.attach(pid);
			int serverPort = this.port;
			
			String agentArgs = "port="+port ;
			vm.loadAgent(agentPath, agentArgs);
			System.out.println("here-----------") ;

		} catch (RuntimeException re) {
			throw re;
		} catch (IOException ioexp) {
			throw ioexp;
		} catch (Exception exp) {
			throw new IOException(exp.getMessage());
		}
	}
}
